package com.chocho.consumeserver.config;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.http.HttpSession;
import javax.websocket.HandshakeResponse;
import javax.websocket.server.HandshakeRequest;
import javax.websocket.server.ServerEndpointConfig;

public class HttpSessionWSHelper extends ServerEndpointConfig.Configurator {

    private Logger logger = LoggerFactory.getLogger(HttpSessionWSHelper.class);

    @Override
    public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
        logger.info("调用modifyHandshake方法...");
        HttpSession session = (HttpSession) request.getHttpSession();//session有可能为空
        if (session!=null){
            logger.debug("获取到session id:"+session.getId());
            sec.getUserProperties().put(HttpSession.class.getName(),session);
        }else{
            logger.warn("modifyHandshake 获取到null session");
        }
    }
}
